package com.wkh.web.job;

import cn.hutool.core.util.StrUtil;
import com.wkh.web.manager.CosManager;
import com.wkh.web.mapper.GeneratorMapper;
import com.wkh.web.model.entity.Generator;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangkehua
 */
@Component
@Slf4j
public class ClearCosJobHandler {
    @Autowired
    private CosManager cosManager;
    @Autowired
    private GeneratorMapper generatorMapper;
    @XxlJob("clearCosJobHandler")
    public void clearCosJobHandler(){
        log.info("clearCosJobHandler....start");
        //业务逻辑
        //每天清理所有无用的文件，包括用户上传的模板制作文件(generator_make_template)
        cosManager.deleteDir("generator_make_template");
        //已删除的代码生成器对应的产物包文件(generator_dist)。
        List<Generator> generators = generatorMapper.ListDeleteGenerator();
        List<String> generatorList = generators.stream().map(Generator::getDistPath)
                .filter(StrUtil::isNotBlank)
                //去除第一个斜杠
                .map(distpth -> distpth.substring(1))
                .collect(Collectors.toList());
        cosManager.deleteObjects(generatorList);
        log.info("clearCosJobHandler....end");
    }
}
